﻿using System.Data;
using System.Data.SqlClient;

namespace Adams.Web.Data.News
{
    public static class Index
    {
        public static Models.News.Index.PageLoad PageLoad(int page, int pagesize)
        {
            var data = new Models.News.Index.PageLoad() { 
                Page = page,
                PageSize = pagesize,
                News = new Shared.Entities.News.ArticleCollection()
            };

            int startrow = (pagesize * (page - 1));
            string dbq = DB.BuildPagingQuery(startrow, pagesize, "*", "News.Articles", string.Empty, string.Empty, "ReleaseDate DESC", "ArticleID");

            using (SqlDataReader dr = DB.RunDataReader(dbq, CommandType.Text))
            {
                while (dr.Read())
                {
                    var Article = DB.InitFromReader<Shared.Entities.News.Article>(dr, new ColMap { NameInDb = "ArticleID", NameInObject = "ID" });
                    data.News.Add(Article);
                }

                dr.NextResult();
                dr.Read();
                data.RecordCount = dr[0].DbInt();
            }

            return data;
        }
    }
}
